/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-10-09
 */
package org.jeecg.modules.flow.callback.inbound.other;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.entity.WmsInReceiptMaterial;
import org.jeecg.modules.business.doc.business.vo.WmsInReceiptPage;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.record.entity.WmsFlowRecord;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.flow.sys.entity.dto.WorkFlowInfoDto;
import org.jeecg.modules.flow.sys.service.WorkFlowService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 横剪入库自动创建入库单并提交
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-10-09
 */
@Component
@Slf4j
public class R19AutoSubmitBeforeBean<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IWmsInReceiptTaskService iWmsInReceiptTaskService;
    @Autowired
    private IWmsInReceiptTaskMaterialService iWmsInReceiptTaskMaterialService;
    @Autowired
    private WorkFlowService workFlowService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        log.info("自动入库任务执行了");
        //根据任务id创建入库单
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        // 获取请求参数
        String taskId = baseFlowDto.getInfoId();
        //任务修改为已完成
        WmsInReceiptTask task = iWmsInReceiptTaskService.getById(taskId);
        task.setDocumentStatus(DocumentStatusEnum.COMPLETE.getCode());
        iWmsInReceiptTaskService.updateById(task);
        //对入库单创建流程提交
        BaseFlowDto req = new BaseFlowDto<>();
        //获取流程code
        WorkFlowInfoDto workFlowInfoDto = new WorkFlowInfoDto();
        workFlowInfoDto.setDocumentType(task.getDocumentType());
        workFlowInfoDto.setBusinessType("document_type");
        List<WmsFlowRecord> flowRecordList = workFlowService.workFlowInfo(workFlowInfoDto);
        //获取提交的code
        req.setCode(flowRecordList.get(flowRecordList.size()-1).getFlowCode());
        req.setBusinessType("document_type");
        req.setDocumentType(task.getDocumentType());
        Date date = new Date();
        WmsInReceiptPage reqData = BeanUtil.copyProperties(task,WmsInReceiptPage.class,"id");
        reqData.setWarehousingDate(date);
        reqData.setCreateTime(date);
        reqData.setHasChild(1);
        reqData.setWmsInReceiptTaskId(taskId);
        reqData.setDocumentType(task.getDocumentType());
        reqData.setDocumentStatus(DocumentStatusEnum.COMPLETE.getCode());
        reqData.setDocumentCode(FillRuleUtil.executeRule("rk_rule",new JSONObject()).toString());
        List<WmsInReceiptMaterial> wmsInReceiptMaterialList = new ArrayList<>();
        //获取入库任务下的物料集合
        List<WmsInReceiptTaskMaterial> materialList = iWmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                .eq(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, taskId)
        );
        int i = 1;
        for (WmsInReceiptTaskMaterial taskMaterial : materialList) {
            WmsInReceiptMaterial inReceiptMaterial = BeanUtil.copyProperties(taskMaterial,WmsInReceiptMaterial.class,"id");
//            one.setWarehousedQuantity(one.getPlanQuantity());
//            one.setUnwarehousedQuantity(BigDecimal.ZERO);
            inReceiptMaterial.setReceivableQuantity(taskMaterial.getPlanQuantity());
            inReceiptMaterial.setWarehouseQuantity(taskMaterial.getPlanQuantity());
            inReceiptMaterial.setUpstreamLineNumber(i+"");
            inReceiptMaterial.setUpstreamDocuments("入库任务");
            inReceiptMaterial.setUpstreamNumber(task.getDocumentCode());
            i++;
            wmsInReceiptMaterialList.add(inReceiptMaterial);
        }
        reqData.setWmsInReceiptMaterialList(wmsInReceiptMaterialList);
        WmsInReceipt wmsInReceipt = new WmsInReceipt();
        BeanUtils.copyProperties(reqData, wmsInReceipt);
        wmsInReceipt.setWmsInReceiptTaskId(taskId);
        //弹窗默认跳过
        reqData.setJumpSecondCheck(true);
        //父任务全部完成,不需要调用回调
        reqData.setIsBackCommit(0);
        req.setData(reqData);
        req.setRequest(baseFlowDto.getRequest());
//        req.setInfoId(id);
        //调用流程提交方法(提交入库单)
        workFlowService.commit(req);
    }
}
